<?
/**
 * VoiceOne Setup
 *
 * Setup of voiceone
 * @package voiceone
 * @subpackage  setup
 */
  session_start();
  $step=$_GET["step"];
  if(($step!="")&&($step!="1")){
  	require("admin/config/cfg.php");
		global $cwlang, $cwLanguage;
  }
/******************************************************************************/
/*************************  LABELS  *****************************/
/******************************************************************************/
  $title					=		"VoiceOne Installation";
  $subTitle				=		"The installation";
  $step1_title		=		"System Setup (1/4)";
  $step1_subtitle	=		"Voiceone Interface Permissions";
  $step2_title		=		"System Setup (2/4)";
  $step2_subtitle	=		"Voiceone Web Services Permissions";
  $step3_title		=		"System Setup (3/4)";
  $step3_subtitle	=		"Voiceone Installation";
  $root_needed		=		"Voiceone has been already installed on the system. ".
   										"To continue the installation you must have superuser privileges. ".
                      "Please fill the form with superuser login.";
  $root_delete		=		"This user will be deleted as well as other settings during installation.";
  $keyGeneration  =   "Generate RSA keys? ";
  $keyHelp 				=		"(if checked key will be generated)";
	$username 			=   "Username"; 
  $password 			=   "Password";  
  $step4_title		=		"System Setup (4/4)";
  $step4_subtitle1=		"System Informations";
  $step4_subtitle2=		"Register Superuser";
  $system_name  	=   "System Name";
  $confirm  			=   "Confirm";
  $language				=   "Language";
  $login_incorect = 	"Login incorrect";
  $sysnameerror =   "Name can not be null";
  $usrerror =   "Username can not be null";
  $pwdempty =   "Password cannot be null";
  $pwderror =   "Password doesn't match";
  $img_ok="<img src=\"admin/public/img/ok.gif\" />";
  $img_no="<img src=\"admin/public/img/shutdown.png\" />";
  
  switch($step){
/******************************************************************************/
/************************  STEP 1: Check Voiceone  ****************************/
/******************************************************************************/
    default:	
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check the config.inc.php permissions
      $alert.="<tr>";
      if(!@is_file("admin/config/config.inc.php")){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Create</strong> configuration file:<br/> <strong>".realpath("admin/config/")."/config.inc.php</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Config File</p></td>";      
      }
      $alert.="</tr>";
      //check the smarty cache folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("admin/public/template/cache") & 0x0004)&&
      			(@fileperms("admin/public/template/cache") & 0x0002)&&
            (@fileperms("admin/public/template/cache") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Smarty Cache folder<br/><strong>".@realpath("admin/public/template/cache")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Smarty Cache folder</p></td>";      
      }
      $alert.="</tr>";
      //check the sandobox folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("sandbox") & 0x0004)&&
      			(@fileperms("sandbox") & 0x0002)&&
            (@fileperms("sandbox") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Sandbox folder<br/><strong>".@realpath("sandbox")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Sandbox folder</p></td>";      
      }
      $alert.="</tr>";
      //check the plugins folder permissions
      $alert.="<tr>";
      if(!(
      			(@fileperms("sandbox/plugins") & 0x0004)&&
      			(@fileperms("sandbox/plugins") & 0x0002)&&
            (@fileperms("sandbox/plugins") & 0x0001)
           )
        ){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Plugin folder<br/> <strong>".@realpath("sandbox/plugins")."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Plugins folder</p></td>";      
      }
      $alert.="</tr>";
      //button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=2';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step1_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step1_subtitle?></h3>
				<table width="100%" cellpadding="15" cellspacing="10">
        	<?=$alert?>
        </table>
        <div style="width:90%;text-align:right;margin-top:20px;">
        	<?=$button?>
        </div>  
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/*********************  STEP 2: Check WebSerivices  ***************************/
/******************************************************************************/
    case "2":
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check interface folders permissions
      if((!is_file("admin/config/config.inc.php"))						||
      	 (!((fileperms("admin/public/template/cache") & 0x0001)&&(fileperms("admin/public/template/cache") & 0x0002)&&(fileperms("admin/public/template/cache") & 0x0004))) ||
      	 (!((fileperms("sandbox") & 0x0001)&&(fileperms("sandbox") & 0x0002)&&(fileperms("sandbox") & 0x0004)))											||
         (!((fileperms("sandbox/plugins") & 0x0001)&&(fileperms("sandbox/plugins") & 0x0002)&&(fileperms("sandbox/plugins") & 0x0004)))){
      	header("location: setup.php?step=1;");
      	die;
      }
     	$config_path="~voiceone_webservices/config/config.inc.php";
      //check the config.inc.php permissions
      $alert.="<tr>";
      if($asterisk===false){
      	$error=true;
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Create</strong> configuration file:<br/> <strong>".$config_path."</strong></p></td>";
      }else{
      	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
        $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Config File</p></td>";      
      }
      $alert.="</tr>";
      //check the script install.sh permissions
      $alert.="<tr>";
      if($asterisk!==false){
      	$ret="";
        $ret=$asterisk->checkDatabaseAccess();
        if($ret!="1"){
        	$error=true;
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\"><strong>Edit</strong> database settings in config file:<br/> <strong>".$config_path."</strong></p></td>";
        }else{
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Access to Voiceone database</p></td>";      
        }
      }
      $alert.="</tr>";
      //check the script install.sh permissions
      $alert.="<tr>";
      if($asterisk!==false){
      	$ret="";
        $ret=$asterisk->isVotoolsExecutable();
        if($ret!="1"){
        	$error=true;
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>execute</strong> permission to script:<br/> <strong>".$ret."</strong></p></td>";
        }else{
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Permissions on Vo-tools script</p></td>";      
        }
      }
      $alert.="</tr>";
      //check the firmware data folder permissions
      /*
      $alert.="<tr>";
      if($asterisk!==false){
      	$ret="";
        $ret=$asterisk->isFirmwareWritable();
        if($ret!="1"){
        	$error=true;
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_no."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:red; font-size:smaller; margin:0;\">Enable the <strong>read/write/execute</strong> permission to Firmware folder:<br/> <strong>".$ret."</strong></p></td>";
        }else{
        	$alert.="<td style=\"width:50px; text-align:center; vertical-align:top;\">".$img_ok."</td>";
          $alert.="<td style=\"padding:0;margin:0; vertical-align:top;\"><p style=\"color:black; font-size:smaller; margin:0;\">Write permission to Firmware folder</p></td>";      
        }
      }
      $alert.="</tr>";
      */
			//button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=3';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step2_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step2_subtitle?></h3>
				<table width="90%" cellpadding="15" cellspacing="10">
        	<?=$alert?>
        </table>
        <div style="width:90%;text-align:right;margin-top:20px;">
        	<?=$button?>
        </div>
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
/******************************************************************************/
/************************  STEP 3: Init Voiceone  *****************************/
/******************************************************************************/
    case "3":
    	//check access to ws
    	if($asterisk===false){
     		header("location: setup.php?step=2");
      	die;
      }
    	//check users
      $userArr=$asterisk->getUser("");
      if((is_array($userArr))&&(count($userArr)!=0)){
      	foreach($userArr as $u){
        	if($u["type"]==3){
           	$superuserArr[]=array("username"=>$u["username"],"password"=>$u["password"]); 
          }
        }
      }
    	//================ POST & GET ================
    	$continue=true;;
      if(count($_POST)>0){
      	if((is_array($superuserArr))&&(count($superuserArr)>0)){ 
        	$continue=false;
      		$user_name=$_POST["username"];
        	$user_pass=$_POST["password"];
          if((trim($user_name)!="")&&(trim($user_pass)!="")){
            foreach($superuserArr as $su){
            	if(($su["username"]==$user_name)&&($su["password"]==$user_pass)){
              	$continue=true;
                break;
              }
            }
          }
      	}
        if($continue===true){
          if(isset($_POST["keys"])) $keys=1; else $keys=0;
          $ret=$asterisk->initialize($keys);
          header("location: setup.php?step=4");
        	die;
       	}
      }
      //================ PAGE LOGIC ================
      $alert="";
      $error=false;
      //check interface folders permissions
      if((!is_file("admin/config/config.inc.php"))								||
         (!((fileperms("admin/public/template/cache") & 0x0002))) ||
      	 (!((fileperms("sandbox") & 0x0002)))											||
         (!((fileperms("sandbox/plugins") & 0x0002)))){
      	header("location: setup.php?step=1");
      	die;
      }
      //check web services permissions
      $ret0=$asterisk->checkDatabaseAccess();
      $ret1=$asterisk->isVotoolsExecutable();
      if(($ret0!="1")||($ret1!="1")){
      	header("location: setup.php?step=2");
      	die;
      }
			//button
      if($error===false) $button="<input type=\"image\" src=\"admin/public/img/en/b-next.gif\" alt=\"Next\" onclick=\"location.href='setup.php?step=3';\" />";
      else $button="<img src=\"admin/public/img/en/b-next_disabled.gif\" alt=\"Next\" />";
      //================== HTML ==================
      ob_start();
      ?>
			<div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>      
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step3_title?></h1>
        <h3 style="margin:0;margin-bottom:10px;padding-left:30px;"><?=$step3_subtitle?></h3>
				<form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <dl>
      		  <dd>
            	<?=$keyGeneration?>
              <input name="keys" type="checkbox" value="1" checked="checked" />
              <span><?=$keyHelp?></span>
            </dd>
          </dl>
          <? if((is_array($superuserArr))&&(count($superuserArr)>0)){ ?>
            <p style="padding:10px 10px 10px 30px;"><?=$root_needed?></p>
            <? if($continue===false){ ?>
            <div style="float:right;width:170px;margin-top:30px;">
            	<img src="admin/public/img/shutdown.png" alt="error" />
            	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$login_incorect?></span>
            </div>
            <? } ?>
            <dl style="padding-left:80px;width:280px;">
              <dt style="width:100px;"><label style="font-weight:bold;"><?=$username?>:</label></dt>
              <dd><input type="text" size="20" name="username"/></dd>
              <dt style="width:100px;"><label style="font-weight:bold;"><?=$password?>:</label></dt>
              <dd><input type="password" size="20" name="password"/></dd>
            </dl>  
        	  <p style="padding:10px 10px 0 30px;"><?=$root_delete?></p>
          <? } ?>
          <!-- Button -->
          <div style="width:90%;text-align:right;margin-top:20px;">
        		<input type="image" src="admin/public/img/en/b-next.gif" alt="Next" />
        	</div>
        </form>
      </div>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;    
/******************************************************************************/
/***************************  STEP 4: Log root  *******************************/
/******************************************************************************/    
    case "4":
    	//check users
      $userArr=$asterisk->getUser("");
      if((is_array($userArr))&&(count($userArr)!=0)){
      	foreach($userArr as $u){
        	if($u["type"]==3){
           	$superuserArr[]=array("username"=>$u["username"],"password"=>$u["password"]); 
          }
        }
      }
     	if((is_array($superuserArr))&&(count($superuserArr)>0)){
      	header("location: setup.php?step=3");
      	die;
      }
    	//================ POST & GET ================
      if(count($_POST)>0) {
        $username=$_POST["username"];
        $password=$_POST["password"];
				$lang=$_POST["lang"];
        $entry=Array(
        		"entity_id" => "0",
        		"username" => "$username", 
            "password" => "$password", 
            "lang" => "$lang",
            "type" => USER_SUPERUSER
        	);
        $ret=$asterisk->putUser($entry);
        $ret=$asterisk->setSystemName($_POST["sysname"]);
        unset($_SESSION["cwSetup"]);
        unset($_SESSION["hostname"]);
        header("location: ./");
        die();
      }
      //================ PAGE LOGIC ================
			//create the language list
      $availLang=getAvailLang();
      foreach($availLang as $l) {
        if ($l=="en") $s="selected=\"selected\""; else $s="";
        $lang.="<option value=\"$l\" $s>$l</option>\n";
      }		
      ob_start();
      ?>
      <div id="loginFirstTime">
        <h1><img src="admin/public/img/VoiceOne-logo.gif" /></h1>
        <h2><?=$title?></h2>
        <p><?=$subTitle?></p>
      </div>   
      <div id="Dialog">
        <h1 style="margin:0;margin-bottom:15px;"><?=$step4_title?></h1>
        <form action="<?echo getenv("REQUEST_URI");?>" method="post">
          <h3 style="margin:0;margin-bottom:0px;padding-left:30px;"><?=$step4_subtitle1?></h3>
          <dl style="padding-left:70px;">
        		<dt style="width:130px;"><label style="font-weight:bold;"><?=$system_name?>:</label></dt>
        		<dd>
            	<input name="sysname" size="20" type="text" id="sysname" value="Voiceone"/>
              <div id="sysname_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />
              	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$sysnameerror?></span>
              </div>
            </dd>
          </dl>
          <h3 style="margin:0;margin-top:20px;padding-left:30px;"><?=$step4_subtitle2?></h3>
        	<dl style="padding-left:100px;">
        		<dt style="width:100px;"><label style="font-weight:bold;"><?=$username?>:</label></dt>
        		<dd>
            	<input name="username" size="20" type="text" id="username" value=""/>
              <div id="user_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />
              	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$usrerror?></span>
              </div>
            </dd>
        		<dt style="width:100px;"><label style="font-weight:bold;"><?=$password?>:</label></dt>
        		<dd>
            	<input name="password" size="20" type="password" id="password" value=""/>
              <div id="pass_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />
              	<span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$pwdempty?></span>
              </div>
            </dd>          
            <dt style="width:100px;"><label style="font-weight:bold;"><?=$confirm?>:</label></dt>
        		<dd>
            	<input name="confirm" size="20" type="password" id="confirm" value=""/> 
              <div id="confirm_check" style="display:none;">
              	<img src="admin/public/img/shutdown.png" alt="error" />	
                <span style="font-size:0.9em;font-weight:bold;color:red;vertical-align:top;"><?=$pwderror?></span>
            	</div>
            </dd>
  					<dt style="width:100px;"><label style="font-weight:bold;"><?=$language?>:</label></dt>
        		<dd><select name="lang" id="lang"><?=$lang?></select></dd>
        	</dl>
        	<!-- Button -->
          <div style="width:90%;text-align:right;margin-top:20px;">
        		<input type="image" src="admin/public/img/en/b-end.gif" alt="Save" onclick=" return checkForm();"/>
        	</div>
        </form>
      </div>     
      <script>
      function checkForm(){
        var returnFalse=false;
        //check System Name
        if(document.getElementById('sysname').value==''){
          document.getElementById('sysname_check').style.display='inline';
          returnFalse=true;
        }else{
        	document.getElementById('sysname_check').style.display='none';
        }
        //check Username
        if(document.getElementById('username').value==''){
          document.getElementById('user_check').style.display='inline';
          returnFalse=true;
        }else{
        	document.getElementById('user_check').style.display='none';
        }
        //check Password
        if(document.getElementById('password').value==''){
          document.getElementById('pass_check').style.display='inline';
          document.getElementById('confirm_check').style.display='none';
          returnFalse=true;
        }else{
        	document.getElementById('pass_check').style.display='none';
          if(document.getElementById('password').value!=document.getElementById('confirm').value) { 
            document.getElementById('confirm_check').style.display='inline';
            returnFalse=true;
          }else{
          	document.getElementById('confirm_check').style.display='none';
          } 
        }
				//return
        if(returnFalse==true) return false;
        else return true;
      }
      </script>
      <?
      $contents=ob_get_contents();
      ob_end_clean();
    break;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
    <title>VoiceOne Setup</title>
    <link rel="shortcut icon" href="admin/public/img/favicon.ico" />
    <link rel="Stylesheet" href="admin/public/css/index.css" type="text/css" media="screen" />
    <script src="admin/public/js/functions.js"></script>
</head>

<body class="login">
  <div class="logincontainer" style="width:600px;">
		<?=$contents?>  
    <div id="Footer">
      <a href="http://www.voiceone.it" target="_blank"><img align="right"  src="admin/public/img/callware.gif" alt="Powered by Callware" border="0" align="absmiddle" hspace="20"/></a>
      <a href="http://www.voiceone.it/support/" target="_blank">Get support</a> | 
      <a href="http://www.voiceone.it/licence/" target="_blank"><?=$loginTerms?></a>
    </div>
  </div>
</body>
</html>